﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

class UtilsMath
{        
    ///	\brief	随机函数，输入一个正整数，返回一个-1~1的伪随机数
    static public float rand(int seed, int x)
    {
        x = (seed * 100000 +x) * 1103515245 + 12345;
        return (((uint)(x / 65536) % 32768) / 32768.0f) * 2.0f - 1;
        //x = (x << 13) ^ x;
        ////float randomVlaue = (1.0 - ((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
        //float randomVlaue = (1.0f - ((x * (x * x * (seed + 15731) + 3) + 1376312589) & 0x7fffffff) / 1073741824.0f);
        //return randomVlaue;
    }  
    ///	\brief	随机函数，输入一个正整数，返回一个0~1的伪随机数
    static public float rand021(int seed, int x)
    {
        return (rand(seed,x) +1) /2f;
    }

    ///	\brief	插值算法
    static public float lerp(float a, float b, float x)
    {
        float ft = x * 3.1415927f;
        float f = (1 - (float)Math.Cos(ft)) * 0.5f;
        return a * (1 - f) + b * f;
    }

    // 根据距离，速度和加速度获取到达目标的时间
    static public float getCostTimeByDistanceSpeedAndSpeedUp(float distance, float speed, float speedUp)
    {
        //t=sqart(2*s/a+pow(v/a,2))-v/a;
        float va = speed / speedUp;
        float t = Mathf.Sqrt(2 * distance / speedUp + Mathf.Pow(va, 2)) - va;
        return t;
    }
}